package com.example.administrator.pandanotes;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.util.List;
/**
 * Created by 于博 on 2018-07-13.
 * 书籍添加界面
 * 有添加按钮，点击添加书籍
 */
public class BooksAddActivity extends AppCompatActivity {
    //变量***********************************************************************
    private Button btnAdd = null;
    private EditText eTClassName = null;
    private EditText eTBookISBN = null;
    private EditText eTBookName = null;
    private EditText eTBookAuthor = null;
    private EditText eTBookPress = null;
    private BookClass bookClass = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_books_add);
        //初始化及绑定******************************************************************
        btnAdd = (Button) findViewById(R.id.btnBooksAdd); //书籍添加按钮
        eTClassName = (EditText)findViewById(R.id.eTBooksAddClassName);//书籍类名
        eTBookISBN = (EditText) findViewById(R.id.eTBooksAddISBN); //书籍isbn
        eTBookName = (EditText) findViewById(R.id.eTBooksAddName);//书籍名
        eTBookAuthor = (EditText) findViewById(R.id.eTBooksAddAuthor);//书籍作者
        eTBookPress = (EditText) findViewById(R.id.eTBooksAddPress);//书籍出版社
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {  //添加按钮点击事件
                //获得值
                String ClassName = eTClassName.getText().toString();
                String BookISBN = eTBookISBN.getText().toString();
                String BookName = eTBookName.getText().toString();
                String BooKAuthor = eTBookAuthor.getText().toString();
                String BookPress = eTBookPress.getText().toString();
                //异常处理
                if(BooKAuthor.length()==0)
                {
                    BooKAuthor="暂无";
                }
                if (BookPress.length()==0)
                {
                    BookPress="暂无";
                }
                List<BookClass> BookClass = DataSupport.where("class_name=?",ClassName).find(BookClass.class);//找到书籍类中所填
                List<Books> books = DataSupport.where("book_ISBN=?",BookISBN).find(Books.class);//找到书籍中所填ISBN
                if (BookClass.size()<=0)
                {
                    Toast.makeText(BooksAddActivity.this,"无此书籍类",Toast.LENGTH_SHORT).show();
                }
                else if(books.size()>0)
                {
                    Toast.makeText(BooksAddActivity.this,"ISBN已存在",Toast.LENGTH_SHORT).show();
                }
                else if(BookISBN.length()!=13)//审核ISBN
                {
                    Toast.makeText(BooksAddActivity.this,"ISBN应该为13位",Toast.LENGTH_SHORT).show();
                }
                else if(BookName.length()<1||BookName.length()>20)//审核书籍名
                {
                    Toast.makeText(BooksAddActivity.this,"书籍名应1-20位",Toast.LENGTH_SHORT).show();
                }
                else if(BooKAuthor.length()<1||BooKAuthor.length()>20)//审核作者名
                {
                    Toast.makeText(BooksAddActivity.this,"作者名应1-20位",Toast.LENGTH_SHORT).show();
                }
                else if(BookPress.length()<1||BookPress.length()>20)//审核出版社名
                {
                    Toast.makeText(BooksAddActivity.this,"出版社应1-20位",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Books book = new Books(BookName,R.drawable.bookclass);
                    book.setClass_name(ClassName);
                    book.setBook_ISBN(BookISBN);
                    book.setBook_author(BooKAuthor);
                    book.setBook_press(BookPress);
                    book.setBook_imageId(R.drawable.bookclass);
                    bookClass = BookClass.get(0);
                    bookClass.getBooksList().add(book);//但愿加到了书籍类里面
                    boolean b = book.save(); //保存
                    if(b)Toast.makeText(BooksAddActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                    else Toast.makeText(BooksAddActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(BooksAddActivity.this,BooksActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
    //返回键调用函数，进行上一页*************************************************************
    @Override
    public void onBackPressed()
    {
        this.finish(); //关闭当前Activity
    }
}
